﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Oscilloscope.Util
{
	// Token: 0x02000002 RID: 2
	public class InternalUtils
	{
		// Token: 0x06000001 RID: 1 RVA: 0x00002050 File Offset: 0x00000250
		private InternalUtils()
		{
		}

		// Token: 0x06000002 RID: 2 RVA: 0x00002058 File Offset: 0x00000258
		public static string GetDisplayStrOfValue(double value)
		{
			double num = Math.Abs(value);
			bool flag = false;
			string text;
			if (num >= 1.0)
			{
				int num2 = 0;
				while (num >= 1000.0)
				{
					num /= 1000.0;
					num2++;
				}
				num = Math.Round(num, 3);
				switch (num2)
				{
				case 0:
					text = string.Empty;
					break;
				case 1:
					text = "K";
					break;
				case 2:
					text = "M";
					break;
				case 3:
					text = "G";
					break;
				case 4:
					text = "T";
					break;
				default:
					flag = true;
					text = "NaN";
					break;
				}
			}
			else
			{
				int num3 = 0;
				if (!InternalUtils.IsEqual(num, 0.0))
				{
					while (num < 1.0)
					{
						num *= 1000.0;
						num3++;
					}
				}
				num = Math.Round(num, 3);
				switch (num3)
				{
				case 0:
					text = string.Empty;
					break;
				case 1:
					text = "m";
					break;
				case 2:
					text = "u";
					break;
				case 3:
					text = "n";
					break;
				default:
					flag = true;
					text = "NaN";
					break;
				}
			}
			if (flag)
			{
				return text;
			}
			if (value >= 0.0 || num == 0.0)
			{
				return num.ToString() + text;
			}
			return "-" + num.ToString() + text;
		}

		// Token: 0x06000003 RID: 3 RVA: 0x000021CC File Offset: 0x000003CC
		internal static double GetIntegralMultipleOfStepBaseValueOf(double value, double step, bool isGreaterThanValue)
		{
			if (value < 0.0 || step <= 0.0)
			{
				throw new ArgumentException();
			}
			double num = value / step;
			if (isGreaterThanValue)
			{
				return Math.Ceiling(num) * step;
			}
			return Math.Floor(num) * step;
		}

		       public static byte ConvertGetByte(byte data, byte data2)
        {
            byte low4 = (byte)data; // 低4位
            byte high4 = (byte)data2;  // 高4位
            // 拼接字节
            byte result_WaveRatio = (byte)((high4 << 4) | low4); // 结果为 0xCF
            return (byte)result_WaveRatio;
        }


		// Token: 0x06000004 RID: 4 RVA: 0x00002210 File Offset: 0x00000410
		internal static void DrawArrow(Graphics g, RectangleF rec, Cursor cursor, Brush arrowColor)
		{
			float num = 0.2f;
			float num2 = 0.5f;
			if (cursor == Cursors.PanSouth)
			{
				InternalUtils.arrowPointsBuffer[0].X = rec.Left + (1f - num) * rec.Width / 2f;
				InternalUtils.arrowPointsBuffer[0].Y = rec.Top;
				InternalUtils.arrowPointsBuffer[1].X = rec.Left + (1f - num) * rec.Width / 2f + num * rec.Width;
				InternalUtils.arrowPointsBuffer[1].Y = rec.Top;
				InternalUtils.arrowPointsBuffer[2].X = rec.Left + (1f - num) * rec.Width / 2f + num * rec.Width;
				InternalUtils.arrowPointsBuffer[2].Y = rec.Top + num2 * rec.Height;
				InternalUtils.arrowPointsBuffer[3].X = rec.Left + rec.Width;
				InternalUtils.arrowPointsBuffer[3].Y = rec.Top + num2 * rec.Height;
				InternalUtils.arrowPointsBuffer[4].X = rec.Left + rec.Width / 2f;
				InternalUtils.arrowPointsBuffer[4].Y = rec.Top + rec.Height;
				InternalUtils.arrowPointsBuffer[5].X = rec.Left;
				InternalUtils.arrowPointsBuffer[5].Y = rec.Top + num2 * rec.Height;
				InternalUtils.arrowPointsBuffer[6].X = rec.Left + (1f - num) * rec.Width / 2f;
				InternalUtils.arrowPointsBuffer[6].Y = rec.Top + num2 * rec.Height;
			}
			else if (cursor == Cursors.PanEast)
			{
				InternalUtils.arrowPointsBuffer[0].X = rec.Left;
				InternalUtils.arrowPointsBuffer[0].Y = rec.Top + rec.Height * (1f - num) / 2f;
				InternalUtils.arrowPointsBuffer[1].X = rec.Left + rec.Width * num2;
				InternalUtils.arrowPointsBuffer[1].Y = rec.Top + rec.Height * (1f - num) / 2f;
				InternalUtils.arrowPointsBuffer[2].X = rec.Left + rec.Width * num2;
				InternalUtils.arrowPointsBuffer[2].Y = rec.Top;
				InternalUtils.arrowPointsBuffer[3].X = rec.Left + rec.Width;
				InternalUtils.arrowPointsBuffer[3].Y = rec.Top + rec.Height / 2f;
				InternalUtils.arrowPointsBuffer[4].X = rec.Left + rec.Width * num2;
				InternalUtils.arrowPointsBuffer[4].Y = rec.Top + rec.Height;
				InternalUtils.arrowPointsBuffer[5].X = rec.Left + rec.Width * num2;
				InternalUtils.arrowPointsBuffer[5].Y = rec.Top + rec.Height - rec.Height * (1f - num) / 2f;
				InternalUtils.arrowPointsBuffer[6].X = rec.Left;
				InternalUtils.arrowPointsBuffer[6].Y = rec.Top + rec.Height - rec.Height * (1f - num) / 2f;
			}
			else if (cursor == Cursors.PanWest)
			{
				InternalUtils.arrowPointsBuffer[0].X = rec.Left + rec.Width;
				InternalUtils.arrowPointsBuffer[0].Y = rec.Top + rec.Height * (1f - num) / 2f;
				InternalUtils.arrowPointsBuffer[1].X = rec.Left + rec.Width;
				InternalUtils.arrowPointsBuffer[1].Y = rec.Top + rec.Height - rec.Height * (1f - num) / 2f;
				InternalUtils.arrowPointsBuffer[2].X = rec.Left + rec.Width * (1f - num2);
				InternalUtils.arrowPointsBuffer[2].Y = rec.Top + rec.Height - rec.Height * (1f - num) / 2f;
				InternalUtils.arrowPointsBuffer[3].X = rec.Left + rec.Width * (1f - num2);
				InternalUtils.arrowPointsBuffer[3].Y = rec.Top + rec.Height;
				InternalUtils.arrowPointsBuffer[4].X = rec.Left;
				InternalUtils.arrowPointsBuffer[4].Y = rec.Top + rec.Height / 2f;
				InternalUtils.arrowPointsBuffer[5].X = rec.Left + rec.Width * (1f - num2);
				InternalUtils.arrowPointsBuffer[5].Y = rec.Top;
				InternalUtils.arrowPointsBuffer[6].X = rec.Left + rec.Width * (1f - num2);
				InternalUtils.arrowPointsBuffer[6].Y = rec.Top + rec.Height * (1f - num) / 2f;
			}
			else if (cursor == Cursors.PanNorth)
			{
				InternalUtils.arrowPointsBuffer[0].X = rec.Left + rec.Width - (1f - num) * rec.Width / 2f;
				InternalUtils.arrowPointsBuffer[0].Y = rec.Top + rec.Height;
				InternalUtils.arrowPointsBuffer[1].X = rec.Left + rec.Width - (1f - num) * rec.Width / 2f;
				InternalUtils.arrowPointsBuffer[1].Y = rec.Top + (1f - num2) * rec.Height;
				InternalUtils.arrowPointsBuffer[2].X = rec.Left + rec.Width;
				InternalUtils.arrowPointsBuffer[2].Y = rec.Top + (1f - num2) * rec.Height;
				InternalUtils.arrowPointsBuffer[3].X = rec.Left + rec.Width / 2f;
				InternalUtils.arrowPointsBuffer[3].Y = rec.Top;
				InternalUtils.arrowPointsBuffer[4].X = rec.Left;
				InternalUtils.arrowPointsBuffer[4].Y = rec.Top + (1f - num2) * rec.Height;
				InternalUtils.arrowPointsBuffer[5].X = rec.Left + (1f - num) * rec.Width / 2f;
				InternalUtils.arrowPointsBuffer[5].Y = rec.Top + (1f - num2) * rec.Height;
				InternalUtils.arrowPointsBuffer[6].X = rec.Left + (1f - num) * rec.Width / 2f;
				InternalUtils.arrowPointsBuffer[6].Y = rec.Top + rec.Height;
			}
			g.FillPolygon(arrowColor, InternalUtils.arrowPointsBuffer);
		}

		// Token: 0x06000005 RID: 5 RVA: 0x00002A88 File Offset: 0x00000C88
		internal static void DrawGrid(Graphics g, Size size)
		{
			float num = (float)size.Width / 10f;
			float num2 = (float)size.Height / 8f;
			Pen pen = new Pen(Color.Gray, 1f);
			for (int i = 0; i < 9; i++)
			{
				if (i == 0)
				{
					pen.Width = 2f;
					pen.DashStyle = DashStyle.Solid;
					g.DrawLine(pen, 0f, (float)i * num2 + 1f, (float)size.Width, (float)i * num2 + 1f);
				}
				else if (i == 8)
				{
					pen.Width = 2f;
					pen.DashStyle = DashStyle.Solid;
					g.DrawLine(pen, 0f, (float)i * num2 - 1f, (float)size.Width, (float)i * num2 - 1f);
				}
				else if (i == 4)
				{
					pen.Width = 2f;
					pen.DashStyle = DashStyle.Dash;
					g.DrawLine(pen, 0f, (float)i * num2, (float)size.Width, (float)i * num2);
				}
				else
				{
					pen.Width = 1f;
					pen.DashStyle = DashStyle.Dash;
					g.DrawLine(pen, 0f, (float)i * num2, (float)size.Width, (float)i * num2);
				}
			}
			for (int j = 0; j < 11; j++)
			{
				if (j == 0)
				{
					pen.Width = 2f;
					pen.DashStyle = DashStyle.Solid;
					g.DrawLine(pen, (float)j * num + 1f, 0f, (float)j * num + 1f, (float)size.Height);
				}
				else if (j == 10)
				{
					pen.Width = 2f;
					pen.DashStyle = DashStyle.Solid;
					g.DrawLine(pen, (float)j * num - 1f, 0f, (float)j * num - 1f, (float)size.Height);
				}
				else if (j == 5)
				{
					pen.Width = 2f;
					pen.DashStyle = DashStyle.Dash;
					g.DrawLine(pen, (float)j * num, 0f, (float)j * num, (float)size.Height);
				}
				else
				{
					pen.Width = 1f;
					pen.DashStyle = DashStyle.Dash;
					g.DrawLine(pen, (float)j * num, 0f, (float)j * num, (float)size.Height);
				}
			}
			pen.Width = 1f;
			pen.DashStyle = DashStyle.Solid;
			float num3 = (float)size.Width / 50f;
			for (int k = 0; k < 51; k++)
			{
				g.DrawLine(pen, (float)k * num3, 0f, (float)k * num3, 4f);
				g.DrawLine(pen, (float)k * num3, (float)size.Height - 5f, (float)k * num3, (float)size.Height);
			}
			float num4 = (float)size.Height / 40f;
			for (int l = 0; l < 41; l++)
			{
				g.DrawLine(pen, 0f, (float)l * num4, 4f, (float)l * num4);
				g.DrawLine(pen, (float)size.Width - 5f, (float)l * num4, (float)size.Width, (float)l * num4);
			}
			pen.Dispose();
		}

		// Token: 0x06000006 RID: 6 RVA: 0x00002DA7 File Offset: 0x00000FA7
		internal static bool IsEqual(double d1, double d2)
		{
			return Math.Abs(d1 - d2) < 1E-13;
		}

		// Token: 0x06000007 RID: 7 RVA: 0x00002DBF File Offset: 0x00000FBF
		internal static bool IsXbeDivByYwithNoRemainder(double x, double y)
		{
			return y != 0.0 && Math.Abs(Math.IEEERemainder(x, y)) < 1E-13;
		}

		// Token: 0x06000008 RID: 8 RVA: 0x00002DEC File Offset: 0x00000FEC
		internal static int InterpolationWithCanvas(PointF p1, PointF p2, int canvasWidth, int canvasHeight, PointF[] newPoints)
		{
			if (canvasWidth <= 0 || canvasHeight <= 0)
			{
				return 0;
			}
			if ((p1.Y < 0f && p2.Y < 0f) || (p1.X < 0f && p2.X < 0f) || (p1.Y > (float)canvasHeight && p2.Y > (float)canvasHeight) || (p1.X > (float)canvasWidth && p2.X > (float)canvasWidth))
			{
				return 0;
			}
			if (newPoints == null)
			{
				throw new ArgumentNullException();
			}
			int num = 0;
			if (p1.X == p2.X)
			{
				InternalUtils.InterpolationBuffer[0].X = p1.X;
				InternalUtils.InterpolationBuffer[0].Y = 0f;
				InternalUtils.InterpolationBuffer[1].X = p1.X;
				InternalUtils.InterpolationBuffer[1].Y = (float)canvasHeight;
				for (int i = 0; i < 2; i++)
				{
					if (((InternalUtils.InterpolationBuffer[i].Y > p1.Y && InternalUtils.InterpolationBuffer[i].Y < p2.Y) || (InternalUtils.InterpolationBuffer[i].Y > p2.Y && InternalUtils.InterpolationBuffer[i].Y < p1.Y)) && InternalUtils.InterpolationBuffer[i].X >= 0f && InternalUtils.InterpolationBuffer[i].X <= (float)canvasWidth && InternalUtils.InterpolationBuffer[i].Y >= 0f && InternalUtils.InterpolationBuffer[i].Y <= (float)canvasHeight)
					{
						newPoints[num].X = InternalUtils.InterpolationBuffer[i].X;
						newPoints[num].Y = InternalUtils.InterpolationBuffer[i].Y;
						num++;
					}
				}
				return num;
			}
			if (p1.Y == p2.Y)
			{
				InternalUtils.InterpolationBuffer[0].X = 0f;
				InternalUtils.InterpolationBuffer[0].Y = p1.Y;
				InternalUtils.InterpolationBuffer[1].X = (float)canvasWidth;
				InternalUtils.InterpolationBuffer[1].Y = p1.Y;
				for (int j = 0; j < 2; j++)
				{
					if (((InternalUtils.InterpolationBuffer[j].X > p1.X && InternalUtils.InterpolationBuffer[j].X < p2.X) || (InternalUtils.InterpolationBuffer[j].X > p2.X && InternalUtils.InterpolationBuffer[j].X < p1.X)) && InternalUtils.InterpolationBuffer[j].X >= 0f && InternalUtils.InterpolationBuffer[j].X <= (float)canvasWidth && InternalUtils.InterpolationBuffer[j].Y >= 0f && InternalUtils.InterpolationBuffer[j].Y <= (float)canvasHeight)
					{
						newPoints[num].X = InternalUtils.InterpolationBuffer[j].X;
						newPoints[num].Y = InternalUtils.InterpolationBuffer[j].Y;
						num++;
					}
				}
				return num;
			}
			InternalUtils.InterpolationBuffer[0].X = 0f;
			InternalUtils.InterpolationBuffer[0].Y = (0f - p1.X) / (p2.X - p1.X) * (p2.Y - p1.Y) + p1.Y;
			InternalUtils.InterpolationBuffer[1].X = (float)canvasWidth;
			InternalUtils.InterpolationBuffer[1].Y = ((float)canvasWidth - p1.X) / (p2.X - p1.X) * (p2.Y - p1.Y) + p1.Y;
			InternalUtils.InterpolationBuffer[2].X = (0f - p1.Y) / (p2.Y - p1.Y) * (p2.X - p1.X) + p1.X;
			InternalUtils.InterpolationBuffer[2].Y = 0f;
			InternalUtils.InterpolationBuffer[3].X = ((float)canvasHeight - p1.Y) / (p2.Y - p1.Y) * (p2.X - p1.X) + p1.X;
			InternalUtils.InterpolationBuffer[3].Y = (float)canvasHeight;
			for (int k = 0; k < 4; k++)
			{
				if (((InternalUtils.InterpolationBuffer[k].X >= p1.X && InternalUtils.InterpolationBuffer[k].X <= p2.X) || (InternalUtils.InterpolationBuffer[k].X >= p2.X && InternalUtils.InterpolationBuffer[k].X <= p1.X)) && ((InternalUtils.InterpolationBuffer[k].Y >= p1.Y && InternalUtils.InterpolationBuffer[k].Y <= p2.Y) || (InternalUtils.InterpolationBuffer[k].Y >= p2.Y && InternalUtils.InterpolationBuffer[k].Y <= p1.Y)) && InternalUtils.InterpolationBuffer[k].X >= 0f && InternalUtils.InterpolationBuffer[k].X <= (float)canvasWidth && InternalUtils.InterpolationBuffer[k].Y >= 0f && InternalUtils.InterpolationBuffer[k].Y <= (float)canvasHeight && num < newPoints.Length)
				{
					newPoints[num].X = InternalUtils.InterpolationBuffer[k].X;
					newPoints[num].Y = InternalUtils.InterpolationBuffer[k].Y;
					num++;
				}
			}
			return num;
		}

		// Token: 0x06000009 RID: 9 RVA: 0x0000344A File Offset: 0x0000164A
		internal static Rectangle GetNormalizedRectangle(Point p1, Point p2)
		{
			return InternalUtils.GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y);
		}

		// Token: 0x0600000A RID: 10 RVA: 0x00003470 File Offset: 0x00001670
		internal static Rectangle GetNormalizedRectangle(int x1, int y1, int x2, int y2)
		{
			if (x2 < x1)
			{
				int num = x2;
				x2 = x1;
				x1 = num;
			}
			if (y2 < y1)
			{
				int num2 = y2;
				y2 = y1;
				y1 = num2;
			}
			return new Rectangle(x1, y1, x2 - x1, y2 - y1);
		}

		// Token: 0x0600000B RID: 11 RVA: 0x000034A2 File Offset: 0x000016A2
		internal static RectangleF GetNormalizedRectangle(PointF p1, PointF p2)
		{
			return InternalUtils.GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y);
		}

		// Token: 0x0600000C RID: 12 RVA: 0x000034C8 File Offset: 0x000016C8
		internal static RectangleF GetNormalizedRectangle(float x1, float y1, float x2, float y2)
		{
			if (x2 < x1)
			{
				float num = x2;
				x2 = x1;
				x1 = num;
			}
			if (y2 < y1)
			{
				float num2 = y2;
				y2 = y1;
				y1 = num2;
			}
			return new RectangleF(x1, y1, x2 - x1, y2 - y1);
		}

		// Token: 0x04000001 RID: 1
		private static readonly PointF[] arrowPointsBuffer = new PointF[7];

		// Token: 0x04000002 RID: 2
		private static readonly PointF[] InterpolationBuffer = new PointF[4];
	}
}
